<script>
import { usePageFrontmatter, useSiteLocaleData, withBase, } from "@vuepress/client";
import { computed, defineComponent, h } from "vue";
import { isString } from "vuepress-shared/client";
import AutoLink from "@theme-hope/components/AutoLink";
import DropTransition from "@theme-hope/components/transitions/DropTransition";
export default defineComponent({
  name: "HeroInfo",
  slots: Object,
  setup(_props, { slots }) {
    const frontmatter = usePageFrontmatter();
    const siteLocale = useSiteLocaleData();
    const isFullScreen = computed(() => frontmatter.value.heroFullScreen ?? false);
    const heroInfo = computed(() => {
      const { heroText, tagline } = frontmatter.value;
      return {
        text: heroText ?? siteLocale.value.title ?? "Hello",
        tagline: tagline ?? siteLocale.value.description ?? "",
        isFullScreen: isFullScreen.value,
      };
    });
    const heroImage = computed(() => {
      const { heroText, heroImage, heroImageDark, heroAlt, heroImageStyle } = frontmatter.value;
      return {
        image: heroImage ? withBase(heroImage) : null,
        imageDark: heroImageDark ? withBase(heroImageDark) : null,
        heroStyle: heroImageStyle,
        alt: heroAlt || heroText || "hero image",
        isFullScreen: isFullScreen.value,
      };
    });
    const bgInfo = computed(() => {
      const { bgImage, bgImageDark, bgImageStyle } = frontmatter.value;
      return {
        image: isString(bgImage) ? withBase(bgImage) : null,
        imageDark: isString(bgImageDark) ? withBase(bgImageDark) : null,
        bgStyle: bgImageStyle,
        isFullScreen: isFullScreen.value,
      };
    });
    const actions = computed(() => frontmatter.value.actions ?? []);
    return () => h("header", { class: ["vp-hero-info-wrapper", { fullscreen: isFullScreen.value }] }, [
      slots.heroBg?.(bgInfo.value) || [
        bgInfo.value.image
            ? h("div", {
              class: ["vp-hero-mask", { light: bgInfo.value.imageDark }],
              style: [
                { "background-image": `url(${bgInfo.value.image})` },
                bgInfo.value.bgStyle,
              ],
            })
            : null,
        bgInfo.value.imageDark
            ? h("div", {
              class: "vp-hero-mask dark",
              style: [
                {
                  "background-image": `url(${bgInfo.value.imageDark})`,
                },
                bgInfo.value.bgStyle,
              ],
            })
            : null,
      ],
      h("div", { class: "vp-hero-info" }, [
        slots.heroImage?.(heroImage.value) ||
        h(DropTransition, { appear: true, type: "group" }, () => [
          heroImage.value.image
              ? h("div", {class: ["vp-hero-image"] ,style: "width: 18rem; max-height: unset;position: relative;"}, [
                h("div", {class: ["vp-hero-image-bg"]}),
                h("img", {
                key: "light",
                class: [
                  "vp-hero-image",
                  { light: heroImage.value.imageDark },
                ],
                style: heroImage.value.heroStyle,
                src: heroImage.value.image,
                alt: heroImage.value.alt,
              })
              ])
              : null,
          heroImage.value.imageDark
              ? h("img", {
                key: "dark",
                class: "vp-hero-image dark",
                style: heroImage.value.heroStyle,
                src: heroImage.value.imageDark,
                alt: heroImage.value.alt,
              })
              : null,
        ]),

        slots.heroInfo?.(heroInfo.value) ??
        h("div", { class: "vp-hero-infos" }, [
          heroInfo.value.text
              ? h(DropTransition, { appear: true, delay: 0.04 }, () => h("h1", { id: "main-title" }, heroInfo.value.text))
              : null,
          heroInfo.value.text
              ? h(DropTransition, { appear: true, delay: 0.04 }, () => h("h1", { id: "main-title-tag" }, ""))
              : null,
          heroInfo.value.text
              ? h(DropTransition, { appear: true, delay: 0.04 }, () => h("h1", { id: "main-title-remark" }, "在线说明文档"))
              : null,
          heroInfo.value.tagline
              ? h(DropTransition, { appear: true, delay: 0.08 }, () => h("p", {
                class: "vp-description",
                innerHTML: heroInfo.value.tagline,
              }))
              : null,
          actions.value.length
              ? h(DropTransition, { appear: true, delay: 0.12 }, () => h("p", { class: "vp-actions" }, actions.value.map((action) => h(AutoLink, {
                class: ["vp-action", action.type || "default"],
                config: action,
                noExternalLinkIcon: true,
              }))))
              : null,
        ]),
      ]),
    ]);
  },
});
</script>
<style lang="scss">
@use "sass:color";
@use "sass:list";
@use "vuepress-shared/styles/shining";

.vp-hero-info-wrapper {
  position: relative;

  display: flex;
  align-items: center;
  justify-content: center;

  // margin-inline: auto;
  margin-inline-start: auto;
  margin-inline-end: auto;

  &.fullscreen {
    height: calc(100vh - var(--navbar-height)) !important;
  }
}

.vp-hero-info {
  z-index: 1;
  width: 100%;

  // NOTE: Compatible with Android Wechat
  // padding-inline: 2.5rem;
  padding-inline-start: 2.5rem;
  padding-inline-end: 2.5rem;

  @media (max-width: hope-config.$pad) {
    // NOTE: Compatible with Android Wechat
    // padding-inline: 1.5rem;
    padding-inline-start: 1.5em;
    padding-inline-end: 1.5rem;
  }

  @media (min-width: hope-config.$pad) {
    display: flex;
    align-items: center;
    justify-content: space-evenly;
  }
}

.vp-hero-mask {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 0;

  background-position: 50%;
  background-size: cover;

  &::after {
    content: " ";

    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;

    display: block;
  }

  &.light {
    display: block;

    #{hope-config.$dark-selector} & {
      display: none;
    }
  }

  &.dark {
    display: none;

    #{hope-config.$dark-selector} & {
      display: block;
    }
  }
}

.vp-hero-infos {
  z-index: 1;
  margin: 0 0.5rem;
}

.vp-hero-image {
  display: block;
  max-width: 100%;
  max-height: 18rem;
  margin: 1rem;

  @media (max-width: hope-config.$pad) {
    margin: 2rem auto;
  }

  @media (max-width: hope-config.$tablet) {
    max-height: 16rem;
    margin: 1.5rem auto;
  }

  @media (max-width: hope-config.$mobile) {
    max-height: 14rem;
  }

  &.light {
    display: block;

    #{hope-config.$dark-selector} & {
      display: none;
    }
  }

  &.dark {
    display: none;

    #{hope-config.$dark-selector} & {
      display: block;
    }
  }
}

#main-title {
  margin: 0.5rem 0;

  background: linear-gradient(
          120deg,
          var(--theme-color-light),
          var(--theme-color) 30%,
          color.adjust(hope-config.$theme-color, $hue: 60deg) 100%
  );
  -webkit-background-clip: text;
  background-clip: text;

  font-weight: bold;
  font-size: 3.6rem;
  font-family: var(--font-family);
  line-height: 1.5;

  -webkit-text-fill-color: transparent;

  @media (max-width: hope-config.$tablet) {
    margin: 0;
  }

  @media (max-width: hope-config.$pad) {
    font-size: 2.5rem;
    text-align: center;
  }

  @media (max-width: hope-config.$tablet) {
    font-size: 2.25rem;
    text-align: center;
  }

  @media (max-width: hope-config.$mobile) {
    margin: 0 auto;
    font-size: 2rem;
  }
}

.vp-description,
.vp-actions {
  margin: 1.8rem 0;

  @media (max-width: hope-config.$tablet) {
    margin: 1.5rem 0;
  }

  @media (max-width: hope-config.$pad) {
    margin: 1.5rem auto;
    text-align: center;
  }

  @media (max-width: hope-config.$mobile) {
    margin: 1.2rem 0;
  }
}

.vp-description {
  max-width: 35rem;

  color: var(--text-color-light);

  font-weight: 500;
  font-size: 1.6rem;
  line-height: 1.3;

  @media (max-width: hope-config.$tablet) {
    font-size: 1.4rem;
  }

  @media (max-width: hope-config.$mobile) {
    font-size: 1.2rem;
  }
}

.vp-action {
  display: inline-block;

  overflow: hidden;

  min-width: 4rem;
  margin: 0.5rem;
  padding: 0.5em 1.5rem;
  border-radius: 2rem;

  background: var(--bg-color-secondary);
  color: var(--text-color);

  font-size: 1.2rem;
  text-align: center;

  transition: color var(--color-transition), color var(--color-transition),
  transform var(--transform-transition);

  @media (max-width: hope-config.$tablet) {
    padding: 0.5rem 1rem;
    font-size: 1.1rem;
  }

  @media (max-width: hope-config.$mobile) {
    font-size: 1rem;
  }

  @media print {
    text-decoration: none;
  }

  &:hover {
    background: var(--bg-color-tertiary);
  }

  &.primary {
    border-color: var(--theme-color);
    background: var(--theme-color);
    color: var(--white);

    // stylelint-disable max-nesting-depth
    &:hover {
      border-color: var(--theme-color-light);
      background: var(--theme-color-light);
    }
    // stylelint-enable max-nesting-depth
  }

  .vp-project-home:not(.pure) &:active {
    transform: scale(0.96);
  }
}
</style>
